package gupao.concurrency.demo;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class ThreadRunInOrder {

    public static void main(String[] args){
        Thread t1 = new Thread(() -> {
            log.info("{} is running...", Thread.currentThread().getName());
            sleepIgnoreExp(20000);
            log.info("{} finish.", Thread.currentThread().getName());
        },  "thread-t1");

        Thread t2 = new Thread(() -> {
            try {
                t1.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }finally{
                log.info("{} is running", Thread.currentThread().getName());
                sleepIgnoreExp(50);
                log.info("{} finish.", Thread.currentThread().getName());
            }
        }, "thread-t2");

        Thread t3 = new Thread(() -> {
            try {
                t2.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }finally{
                log.info("{} is running", Thread.currentThread().getName());
                sleepIgnoreExp(50);
                log.info("{} finish.", Thread.currentThread().getName());
            }
        }, "thread-t3");

        t3.start();
        t2.start();
        t1.start();
    }


    public static void sleepIgnoreExp(long millis){
        try {
            Thread.sleep(millis);
        } catch (InterruptedException e) {
        }
    }
}
